package com.dupengcheng.oss.controller;

import com.dupengcheng.oss.entity.Result;
import com.dupengcheng.oss.service.FileService;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
public class FileController {

    @Resource
    private FileService fileService;

    @RequestMapping("/getAllBucket")
    public Result<String> getAllBucket(){

        String allBucket = fileService.getAllBucket();


        return Result.ok(allBucket);

    }
    @RequestMapping("/uploadFile")
    public Result<Boolean> uploadFile(MultipartFile file, String bucketName, String objectName){
        try {

            if (StringUtils.isEmpty(objectName)){
                objectName=file.getOriginalFilename();
            }



            fileService.uploadFile(file,bucketName,objectName);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }


    }

}
